<?php
/**
 * 小程序我的钱包
 */
namespace app\adminweb\controller;
header("Access-Control-Allow-Origin: * ");
header("Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE");
class Wallet extends Pre
{
    //当前我的下级结佣列表
    public function lists(){
        $data = input('param.');
        $loginInfo = $this -> loginInfo;
        //待提现
        $map[] = ['account_id','eq',$loginInfo['id']];
        $map[] = ['status','eq',0];
        $wait = \app\common\model\Wallet::where($map)->select();
        //已结佣
        $map1[] = ['account_id','eq',$loginInfo['id']];
        $map1[] = ['status','eq',1];
        $complete = \app\common\model\Wallet::where($map1)->select();
        return [
            'content' => $data['mark']==1?$complete:$wait,
            'num' => count($wait),
            'numed' => count($complete)
        ];
    }
    //我的财务管理页面
    public function finance(){
        $data = input('param.');
        $loginInfo = $this -> loginInfo;
        if($data['status'] !== null){
            $map[] = ['status','eq',$data['status']];
        }else{
            $map[] = ['status','eq','0'];
        }
        if($data['key']){
            $data['key'] = trim($data['key']);
            $map1[] = ['name|telephone','like',"%$data[key]%"];
            $re = \app\common\model\Message::where($map1) -> find();
            $map[] = ['message_id','eq',$re['id']];
            $data['page'] = 1;
        }else{
            $map[] = ['account_id','eq',$loginInfo['id']];
        }
        $result = \app\common\model\Wallet::
                    with('message,account,followInfo,dealInfo,transferInfo')
                  -> where($map) -> order('id DESC') -> page($data['page'],$data['limit'])
                  ->select() -> toArray();
        foreach ($result as $val){
            $array['id'] = $val['id'];
            $array['name'] = $val['account']['name'];
            $array['telephone'] = $val['account']['telephone'];
            $array['type'] = $val['message']['house1'].'室'
                .$val['message']['house2'].'厅'
                .$val['message']['house3'].'卫'.$val['message']['house4'].'厨';
            $array['number'] = $val['message']['number'];
            $array['price'] = $val['message']['price'];
            $array['date'] = date('Y-m-d h:i:s',$val['date']);
            $array['count'] = $val['count'];
            $array['status'] = $val['message']['status'];
            $build = \app\common\model\Building::get($val['message']['building']);
            $array['building'] = $build['name'];
            $array['sex'] = $val['message']['sex'];
            $array['name1'] = $val['message']['name'];
            $array['telephone1'] = $val['message']['telephone'];
            $array['schedule'] = date('Y-m-d H:i:s',$val['schedule']);
            if($val['follow_info']){
                foreach ($val['follow_info'] as $va){
                    $da = \app\common\model\Account::get($va['message_id']);
                    $tmp['id'] = $va['id'];
                    $tmp['content'] = $va['content'];
                    $tmp['message_id'] = $va['message_id'];
                    $tmp['account_id'] = $da['name'];
                    $th = \app\common\model\Role::get($da['role']);
                    $tmp['role'] = $th['rolename'];
                    $tmp['date'] = date('Y-m-d H:i:s',$va['date']);
                    $tm[] = $tmp;
                }
            }else{
                $tm = array();
            }
            if($val['transfer_info']){
                $name = \app\common\model\Account::with('roleInfo')
                        -> where('id',$val['transfer_info']['to_account_id'])
                        -> find();
                $val['transfer_info']['to_account_id'] = $name['name'];
                $val['transfer_info']['role'] = $name['role_info']['rolename'];
                $val['transfer_info']['date'] =
                    date('Y-m-d H:i:s',$val['transfer_info']['date']);

            }
            if($val['deal_info']){
                foreach ($val['deal_info'] as $k=>$valu){
                    $val['deal_info'][$k]['img'] = 'https://show.enterlo.com'.$valu['img'];
                }
            }
            if($val['deal_info']){
                $array['deal_info'] = $val['deal_info'];
            }else{
                $array['deal_info'] = array();
            }
            $array['follow_info'] = $tm;
            $array['transfer_info'] = $val['transfer_info'];
            $array1[] = $array;
            unset($array);
            unset($tm);
        }
        $count = \app\common\model\Wallet::where($map) -> count();
        $array = array('code'=>0,'msg' =>'','count'=>$count,'data'=>$array1);
        return $array;
    }
    //管理员财务管理
    public function adminpay(){
        $data = input('param.');
        if($data['status']){
            $map[] = ['status','eq',$data['status']];
        }else{
            $map[] = ['status','eq','已定'];
        }
        if($data['key']){
            $data['key'] = trim($data['key']);
            $map1[] = ['name|telephone','like',"%$data[key]%"];
            $re = \app\common\model\Message::where($map1) -> find();
            $map[] = ['id','eq',$re['id']];
            $data['page'] = 1;
        }
        $result = \app\common\model\Message::where($map)
                    -> with('accountInfo,followInfo,dealInfo,transferInfo')
                    -> order('id DESC')
                    ->page($data['page'],$data['limit'])-> select();
        if($result)
            $result -> toArray();
        foreach ($result as $val){
            $array['id'] = $val['id'];
            $array['name'] = $val['account_info']['name'];
            $array['telephone'] = $val['account_info']['telephone'];
            $array['type'] = $val['house1'].'室'
                .$val['house2'].'厅'
                .$val['house3'].'卫'.$val['house4'].'厨';
            $array['number'] = $val['number'];
            $array['price'] = $val['price'];
            $array['date'] = date('Y-m-d H:i:s',$val['time']);
            $array['status'] = $val['status'];
            $array['price_end'] = $val['price_end'];
            $build = \app\common\model\Building::get($val['building']);
            $array['building'] = $build['name'];
            $array['sex'] = $val['sex'];
            $array['name1'] = $val['name'];
            $array['telephone1'] = $val['telephone'];
            $array['schedule'] = date('Y-m-d H:i:s',$val['schedule']);
            if(count($val['follow_info']) !== 0){
                foreach ($val['follow_info'] as $va){
                    $da = \app\common\model\Account::with('roleInfo')
                        -> where('id',$va['account_id']) -> find();
                    if($da)
                        $da -> toArray();
                    $tmp['id'] = $va['id'];
                    $tmp['content'] = $va['content'];
                    $tmp['message_id'] = $va['message_id'];
                    $tmp['wang'] = $da['name'];
                    $tmp['role'] = $da['role_info']['rolename'];
                    $tmp['date'] = date('Y-m-d H:i:s',$va['date']);
                    $tm[] = $tmp;
                }
            }else{
                $tm = array();
            }
            if($val['transfer_info']){
                $name = \app\common\model\Account::with('roleInfo')
                    -> where('id',$val['transfer_info']['to_account_id'])
                    -> find() -> toArray();
                $val['transfer_info']['to_account_id'] = $name['name'];
                $val['transfer_info']['role'] = $name['role_info']['rolename'];
                $val['transfer_info']['date'] =
                    date('Y-m-d H:i:s',$val['transfer_info']['date']);

            }
            $array['follow_info'] = $tm;
            if($val['deal_info']){
                foreach ($val['deal_info'] as $valu){
                        $valu['img'] = 'https://show.enterlo.com'.$valu['img'];
                }
            }
            if($val['deal_info']){
                $array['deal_info'] = $val['deal_info']-> toArray();
            }else{
                $array['deal_info'] = array();
            }
            if($val['transfer_info']){
                $array['transfer_info'] = $val['transfer_info'] ->toArray();
            }else{
                $array['transfer_info'] = array();
            }
            $array1[] = $array;
            unset($array);
            unset($tm);
        }
        $count = \app\common\model\Message::where($map) -> count();
        $arrayinfo = array('code'=>0,'msg' =>'','count'=>$count,'data'=>$array1);
        return $arrayinfo;
    }
    //佣金详细信息
    public function detail(){
        $data = input('param.');
        $result = \app\common\model\Wallet::get($data['id']);
        if($result){
            $map[] = ['message_id','eq',$result['message_id']];
            $result1 = \app\common\model\Wallet::where($map) ->page($data['page'],$data['limit'])
                -> select()-> toArray();
            foreach ($result1 as $value){
                $result2 = \app\common\model\Account::where('id',$value['account_id'])-> find();
                $value['name'] = $result2['name'];
                $value['telephone'] = $result2['telephone'];
                $result3 = \app\common\model\Role::where('id','eq',$result2['role']) -> find();
                $value['position'] = $result3['rolename'];
                $value['status'] = $value['status']?'已提现':'未提现';
                $value['date'] = date('Y-m-d H:i:s',$value['date']);
                $tmp[] = $value;
            }
        }else{
            $tmp = array();
        }
        $array = array('code'=>0,'msg' =>'','count'=>count($tmp),'data'=>$tmp);
        return $array;
    }

    //管理员结佣方法
    public function inserts(){
        $wallet =new \app\common\model\Wallet();
        return $wallet ->inserts();
    }

    //管理员取消结佣
    public function dels(){
        $wallet =new \app\common\model\Wallet();
        return $wallet ->dels();
    }

}